import 'package:flutter/material.dart';
import 'package:interview/config/app.enum.dart';
import 'package:interview/servers/question_type.dart';
import 'package:interview/theme/app_colors.dart';

class CategoryTabbar extends StatelessWidget {
  final List<QuestionType> categoryList;
  final ThemeData theme;
  final TabController tabController;
  final void Function(int) onTabTapped;
  final VoidCallback onFilterTapped;

  const CategoryTabbar({
    super.key,
    required this.categoryList,
    required this.theme,
    required this.tabController,
    required this.onTabTapped,
    required this.onFilterTapped,
  });

  @override
  Widget build(BuildContext context) {
    return Row(
      children: [
        // TabBar 部分
        Expanded(
          child: TabBar(
            tabAlignment: TabAlignment.start,
            controller: tabController,
            indicatorColor: theme.extension<AppColors>()!.contrastPure, // 选中状态下划线
            dividerHeight: 0, // 整个 TabBar 的分割线
            isScrollable: true,
            unselectedLabelStyle: TextStyle(
              fontSize: 10,
              fontWeight: FontWeight.normal,
              color: theme.extension<AppColors>()!.contrastMix1!,
            ),
            labelStyle: TextStyle(
              fontSize: 11,
              fontWeight: FontWeight.bold,
              color: theme.extension<AppColors>()!.contrastPure!,
            ),
            onTap: onTabTapped,
            tabs: categoryList.map((category) {
              return Tab(
                child: Row(
                  mainAxisSize: MainAxisSize.min, // 避免占用多余的空间
                  children: [
                    Text(
                      category.name,
                      style: const TextStyle(fontSize: 12),
                    ),
                    if (category.displayNewestFlag == 1) // 条件渲染
                      Padding(
                        padding: const EdgeInsets.only(left: 2), // 图标与文字的间距
                        child: Image.asset(
                          '${AppEnumAssets.basePath}ic_home_new.png',
                          width: 30,
                          height: 18,
                        ),
                      ),
                  ],
                ),
              );
            }).toList(),
          ),
        ),
        // 筛选按钮部分
        Padding(
          padding: const EdgeInsets.only(right: 8), // 按钮右侧间距
          child: IconButton(
            icon: Image.asset(
              '${AppEnumAssets.basePath}ic_home_filter.png',
              width: 24,
              height: 24,
            ),
            onPressed: onFilterTapped,
          ),
        ),
      ],
    );
  }
}